package yyy.ab.logging.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import yyy.ab.common.core.BaseController;
import yyy.ab.common.core.ResultBean;
import yyy.ab.logging.entity.SysLog;
import yyy.ab.logging.service.SysLogService;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequiredArgsConstructor
@Api(tags = "系统：日志管理")
public class SysLogController extends BaseController {

    private final SysLogService service;

    /**
     * 操作日志页面
     */
    @RequestMapping("/monitor/log")
    //@PreAuthorize("@ab.check('log:list')")
    public ModelAndView jump() {
        ModelAndView view = new ModelAndView();
        view.setViewName("monitor/log/index");
        return view;
    }

    /**
     * 异常日志页面
     */
    @RequestMapping("/monitor/errorLog")
    //@PreAuthorize("@ab.check('log:list')")
    public ModelAndView errorJump() {
        ModelAndView view = new ModelAndView();
        view.setViewName("monitor/log/error");
        return view;
    }

    //    @Log("查询日志")
    @ApiOperation("查询日志")
    @GetMapping("/monitor/log/findPage")
    public Object query(@RequestParam Map<String, Object> params) {
        IPage<SysLog> page = service.findPage(params);
        return jsonForTable(page);
    }

    @ApiOperation("查询单个日志")
    @GetMapping("/monitor/log/get")
    public SysLog get(Long id) {
        SysLog log = service.getById(id);
        return log;
    }

    @ApiOperation("删除日志")
    @DeleteMapping("/monitor/log/del")
    public Object deleteAll(String logType) {
        QueryWrapper<SysLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("log_type",logType);
        service.remove(queryWrapper);
        return ResultBean.success();
    }
}
